import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { FormBuilder } from '@angular/forms';
import { IDiscloseTitle } from '@model/disclose';
import { DiscloseService } from '@service/disclose.service';
import { TreeService } from '@service/tree.service';
import { SystemService } from '@service/system.service';
import { deepClone } from '@utils/lang';
import { AuthService } from '@service/auth.service';
const ID = '123456789';


@Component({
	selector: 'app-disclose-title',
	templateUrl: './disclose-title.component.html',
	styleUrls: ['./disclose-title.component.less']
})
export class DiscloseTitleComponent extends AbsFormUtil implements OnInit {

	nzLoading = false;
	data = [];
	editData: IDiscloseTitle = null;
	departOption = [];
	departList = [];
	constructor(
		private discloseService: DiscloseService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private systemService: SystemService,
		private treeService: TreeService,
		private authService: AuthService

	) {
		super();
	}

	ngOnInit() {
		this.authList = this.authService.getAuth();
		this.buildForm();
		this.onSearch();

		this.getDepartList();
	}
	private async getDepartList() {
		const result = await this.systemService.getDepartList();
		this.departList = result.dept;
		this.departList.forEach(it => {
			it.label = it.deptName;
			it.value = String(it.id);
			it.isLeaf = !this.departList.some(itm => itm.parentId == it.id);
		});
		this.departOption = this.treeService.convertListToTree(this.departList, 0);
	}

	
	buildForm() {
		this.validateForm = this.fb.group({
			discloseTitle: [null]
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		this.editData = null;
		if (isPageInit) {
			this.page.page = 1;
		}
		const params = { ...this.validateForm.value, ...this.page };
		const result = await this.discloseService.getDiscloseTitleList(params);
		this.nzLoading = false;
		this.data = result.data.list;
		this.page.total = result.data.totalCount;
	}



	create() {
		if (this.editData && this.editData.id) {
			this.nzMessageService.info('请一次编辑一条数据');
			return;
		}
		this.editData = new IDiscloseTitle();
		this.editData.id = ID;
		this.data.unshift(this.editData);
		this.data = [...this.data];
	}
	async del(item) {
		await this.discloseService.deleteDiscloseTitle(item.id);
		this.nzMessageService.success('删除成功');
		this.onSearch(true);
	}
	cancel() {
		if (this.editData.id === ID) {
			this.data.shift();
			this.data = [...this.data];
		}
		this.editData = null;
	}
	edit(item: IDiscloseTitle) {
		if (this.editData && this.editData.id) {
			this.nzMessageService.info('请一次编辑一条数据');
			return;
		}
		if (item) {
			const deptItem = this.departList.find(itm => itm.value === String(item.deptId));
			item.deptId = deptItem ? this.getDepartId(deptItem, [deptItem.value]) : [];
		}
		this.editData = deepClone(item);
	}

	getDepartId(depart, result: string[]) {
		const departItem = this.departList.find(itm => itm.value === String(depart.parentId));
		if (departItem) {
			result.unshift(departItem.value);
			if (departItem.parentId) {
				return this.getDepartId(departItem, result);
			}
			return result;
		}
	}


	async save(item: IDiscloseTitle) {
		if (!item.discloseTitle) {
			this.nzMessageService.error('爆料主题不能为空');
			return;
		}
		if (!item.dept?.length) {
			this.nzMessageService.error('部门不能为空');
			return;
		}
		const editData = JSON.parse(JSON.stringify(this.editData));
		const isCreate = editData.id === ID;
		if (isCreate) {
			editData.id = '';
		}
		editData.deptId = editData.deptId ? editData.deptId.pop() : '';
		const deptItem = this.departList.find(itm => itm.value === editData.deptId);
		editData.dept = editData.deptId;
		const result = await this.discloseService.saveDiscloseTitle(editData)
		if (result.status !== 0) {
			this.nzMessageService.error(result.msg);
			return;
		}
		editData.dept = deptItem.deptName;
		isCreate ? this.onSearch(true) : Object.assign(item, editData);
		this.editData = new IDiscloseTitle();
		this.nzMessageService.success(isCreate ? '新增成功' : '修改成功');
	}

}
